Grokking LinkedIn Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
LinkedIn Easy
Find Pivot Index (easy)
Solution: Find Pivot Index
Isomorphic Strings (easy)
Solution: Isomorphic Strings
Second Minimum Node In a Binary Tree (easy)
Solution: Second Minimum Node In a Binary Tree
Minimum Time to Type Word Using Special Typewriter (easy)
Solution: Minimum Time to Type Word Using Special Typewriter
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Can Place Flowers (easy)
Solution: Can Place Flowers
Valid Perfect Square (easy)
Solution: Valid Perfect Square
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Sqrt (easy)
Solution: Sqrt
Valid Palindrome (easy)
Solution: Valid Palindrome
LinkedIn Medium
Rotate List (medium)
Solution: Rotate List
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Valid Triangle Number (medium)
Solution: Valid Triangle Number
Valid Parenthesis String (medium)
Solution: Valid Parenthesis String
Edit Distance (medium)
Solution: Edit Distance
Remove All Ones With Row and Column Flips II (medium)
Solution: Remove All Ones With Row and Column Flips II
Reverse Integer (medium)
Solution: Reverse Integer
Triplet Sum to Zero (medium)
Solution: Triplet Sum to Zero
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Decode Ways (medium)
Solution: Decode Ways
Quadruple Sum to Target (medium)
Solution: Quadruple Sum to Target
Next Permutation (medium)
Solution: Next Permutation
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Word Search (medium)
Solution: Word Search
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
House Robber II (medium)
Solution: House Robber II
Coin Change
Solution: Coin Change
Design Add and Search Words Data Structure (medium)
Solution: Design Add and Search Words Data Structure
Merge Intervals (medium)
Solution: Merge Intervals
Number of Islands (easy)
Solution: Number of Islands
Longest Palindromic Subsequence (medium)
Solution: Longest Palindromic Subsequence
Kth Smallest Number (medium)
Solution: Kth Smallest Number
Maximum Product Subarray (medium)
Solution: Maximum Product Subarray
Same Tree (medium)
Solution: Same Tree
Max Points on a Line (hard)
Solution: Max Points on a Line
Closest Binary Search Tree Value II (hard)
Solution: Closest Binary Search Tree Value II
Word Ladder (hard)
Solution: Word Ladder
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Solution: Reverse Integer

Problem Statement

Given a signed 32-bit integer x, return the reverse of x. If the reversed value of x goes beyond the signed 32-bit integer range [-231, 231 - 1], then return 0.

A reverse of x should not contain leading zeros.

Examples

  • Example 1:

    • Input: x = 123
    • Expected Output: 321
    • Justification: Reversing the digits of 123 results in 321, which is within the 32-bit signed integer range.
  • Example 2:

    • Input: x = -456432
    • Expected Output: -234654
    • Justification: Reversing the digits of -456432 results in -234654, maintaining the negative sign and staying within the 32-bit signed integer range.
  • Example 3:

    • Input: x = 1200
    • Expected Output: 21
    • Justification: Reversing the digits of 1200 results in 0021, but leading zeros are dropped, resulting in 21.

Solution

To solve this problem, we'll take a mathematical approach to reverse the digits of the given integer. This approach is effective because it directly manipulates the numerical value, allowing us to easily handle both positive and negative numbers and check for overflow conditions. The essence of the solution is to iteratively extract the last digit of the current number, append it to the reversed number, and then remove that digit from the original number by division. This process repeats until all digits have been processed.

The key to handling overflow is to check if the new reversed number, when multiplied back by 10 (to add a new digit), would exceed the 32-bit integer limit. By performing this check before actually appending the next digit, we can ensure that our function never produces an overflowed result. This method is not only straightforward but also efficient, as it requires no additional data structures and operates in linear time complexity relative to the number of digits in the input.

Step-by-step Algorithm

  • Initialize a variable reversed to store the reversed number, starting at 0.
  • While the input number x is not 0:
    • Extract the last digit of x by taking x % 10.
    • Check for overflow/underflow before appending the digit to reversed. If appending would cause overflow or underflow, return 0.
    • Append the digit to reversed by multiplying reversed by 10 and adding the extracted digit.
    • Remove the last digit from x by dividing it by 10.
  • After the loop, if reversed is within the 32-bit signed integer range, return reversed. Otherwise, return 0.

Algorithm Walkthrough

Let's consider the Input: -456432

  1. Initialize Variables:

    • Set reversed to 0. This variable will hold our reversed number as we build it.
    • The input x is (-456432).
  2. Iteration 1:

    • Extract the last digit: (x % 10 = -2).
    • Append -2 to reversed: (reversed = reversed * 10 - 2 = -2).
    • Update x by removing the last digit: (x = x / 10 = -45643).
  3. Iteration 2:

    • Extract the last digit: (x % 10 = -3).
    • Append -3 to reversed: (reversed = -2 * 10 - 3 = -23).
    • Update x: (x = x / 10 = -4564).
  4. Iteration 3:

    • Extract the last digit: (x % 10 = -4).
    • Append -4 to reversed: (reversed = -23 * 10 - 4 = -234).
    • Update x: (x = x / 10 = -456).
  5. Iteration 4:

    • Extract the last digit: (x % 10 = -6).
    • Append -6 to reversed: (reversed = -234 * 10 - 6 = -2346).
    • Update x: (x = x / 10 = -45).
  6. Iteration 5:

    • Extract the last digit: (x % 10 = -5).
    • Append -5 to reversed: (reversed = -2346 * 10 - 5 = -23465).
    • Update x: (x = x / 10 = -4).
  7. Iteration 6:

    • Extract the last digit: (x % 10 = -4).
    • Append -4 to reversed: (reversed = -23465 * 10 - 4 = -234654).
    • Update x: (x = x / 10 = 0).
  8. Final Check and Return:

    • At this point, x is 0, so we exit the loop.
    • Return reversed, which is (-234654).

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity of the reverse integer algorithm is , where (n) is the value of the input integer. This is because the algorithm processes each digit of the input number exactly once, and the number of digits in a number (n) is proportional to .

Space Complexity

The space complexity of the algorithm is , indicating constant space usage. This is because the algorithm uses a fixed amount of space (a few variables) regardless of the input size.

Reverse Integer (medium)
Triplet Sum to Zero (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity